import { Router } from "express";
import { IcallbackFn, Ioracle } from "../../types";

export default (router: Router, oracle: Ioracle, callbackFn: IcallbackFn) => {

  //抓取需要新增
  router.post("/getNewDormData", (_req, res) => {
    let sql = " select work_num,chinese_name,first_flag,dorm_num "
    sql += " from xx_g00_dormitory_face_data "
    sql += " where 1 = 1 "
    sql += " and post_flag = 'N' "
    sql += " and liveout_flag is null "
    sql += " and old_dorm is null "
    callbackFn( res, oracle.connChaos,{sql})
  })

  //抓取退宿人员
  router.post("/getDeleteDormData", (_req, res) => {
    let sql = " select a.work_num,case when a.no_out_but_delete_flag = 'Y' then a.old_dorm else a.dorm_num end as dorm_num  from xx_g00_dormitory_face_data a "
    sql += " where 1 = 1 "
    sql += " and a.post_flag = 'N' "
    sql += " and a.liveout_flag = 'Y' "
    sql += " and a.dorm_num <> 'X' "
    sql += " union"
    sql += " select a.work_num,a.old_dorm dorm_num "
    sql += " from xx_g00_dormitory_face_data a "
    sql += " where 1 = 1  "
    sql += " and a.post_flag = 'N' "
    sql += " and a.liveout_flag = 'Y' "
    sql += " and a.dorm_num = 'X' "
    callbackFn( res, oracle.connChaos,{sql})
  })

  //抓取换宿舍人员
  router.post("/getModifyDormData", (_req, res) => {
    let sql = " select work_num,a.dorm_num,a.old_dorm "
    sql += " from xx_g00_dormitory_face_data a "
    sql += " where 1 = 1 "
    sql += " and a.post_flag = 'N' "
    sql += " and a.old_dorm is not null "
    sql += " and a.liveout_flag is null "
    callbackFn(res, oracle.connChaos,{sql})
  })

  //更新已抛
  router.post("/updateDormData",(req,res) => {
    const { result,work_num } = req.body
    let sql = " update xx_g00_dormitory_face_data "
    sql += " set post_flag = 'Y' "
    sql += " ,updated_date = sysdate "
    sql += " ,post_result = '" + result + "' "
    sql += " where work_num = '" + work_num + "' "
    callbackFn( res, oracle.connChaos,{sql})
  })

  //更新资料
  router.post("/refreshDormData",(_req,res) => {
    let sql = " begin xx_g00_dormitory_face_data_p;end; "
    callbackFn(res, oracle.connChaos,{sql})
  })

  //抓取所有资料
  router.post("/getDormData",(_req,res) => {
    let sql = " select * from xx_g00_dormitory_face_data "
    sql += " order by dorm_num,floor_num "
    callbackFn(res, oracle.connChaos,{sql})
  })

   //抓取c3資料--工程師宿舍住五樓
   router.post("/getC3Data",(_req,res)=>{
    let sql = "select work_num from xx_g00_dormitory_face_data"
    sql += " where c3_post_flag = 'N'"
    sql += " and c3_flag ='Y'"
    callbackFn(res,oracle.connChaos,{sql})
  })
  //抓取删除c3资料
  router.post("/getDelC3Data",(_req,res)=>{
    let sql = "select work_num from xx_g00_dormitory_face_data"
    sql += " where c3_post_flag = 'N'"
    sql += " and c3_flag is null"
    callbackFn(res,oracle.connChaos,{sql})
  })

  //更新c3拋出接口成功資料
  router.post("/updateC3Data",(req,res)=>{
    const{work_num,operation} = req.body;
    //增加c3
    if(operation == "add"){
      let sql = "update xx_g00_dormitory_face_data"
      sql += " set c3_post_flag = 'Y'"
      sql += " ,c3_update_time = sysdate"
      sql += " where work_num = '" + work_num + "'"
      callbackFn(res,oracle.connChaos,{sql})
    }
    //刪除c3
    else if (operation == "delete"){
      let sql = "update xx_g00_dormitory_face_data"
      sql += " set c3_post_flag = null"
      sql += " ,c3_update_time = sysdate"
      sql += " where work_num = '" + work_num + "'"
      callbackFn(res,oracle.connChaos,{sql})
    }
  })

  return router
}